home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_apache2.idb / usr / freeware / apache2 / include / apr_atomic.h.z / apr_atomic.h
C/C++ Source or Header  |  2002-07-08  |  9KB  |  251 lines

  1. /* ====================================================================
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Apache" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  * Portions of this software are based upon public domain software
  55.  * originally written at the National Center for Supercomputing Applications,
  56.  * University of Illinois, Urbana-Champaign.
  57.  */
  58.  
  59. #ifndef APR_ATOMIC_H
  60. #define APR_ATOMIC_H
  61.  
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65.  
  66. #include "apr_pools.h"
  67.  
  68. /**
  69.  * @file apr_atomic.h
  70.  * @brief APR Atomic Operations
  71.  */
  72. /**
  73.  * @defgroup APR_Atomic Atomic operations
  74.  * @ingroup APR
  75.  * @{
  76.  */
  77.  
  78. /* easiest way to get these documented for the moment */
  79. #if defined(DOXYGEN)
  80. /**
  81.  * structure for holding a atomic value.
  82.  * this number >only< has a 24 bit size on some platforms
  83.  */
  84. typedef apr_atomic_t;
  85.  
  86. /**
  87.  * @param pool 
  88.  * this function is required on some platforms to initialize the
  89.  * atomic operation's internal structures
  90.  * returns APR_SUCCESS on successfull completion
  91.  */
  92. apr_status_t apr_atomic_init(apr_pool_t *p);
  93. /**
  94.  * read the value stored in a atomic variable
  95.  * @param the pointer
  96.  * @warning on certain platforms this number is not stored
  97.  * directly in the pointer. in others it is 
  98.  */
  99. apr_uint32_t apr_atomic_read(volatile apr_atomic_t *mem);
  100. /**
  101.  * set the value for atomic.
  102.  * @param the pointer
  103.  * @param the value
  104.  */
  105. void apr_atomic_set(volatile apr_atomic_t *mem, apr_uint32_t val);
  106. /**
  107.  * Add 'val' to the atomic variable
  108.  * @param mem pointer to the atomic value
  109.  * @param val the addition
  110.  */
  111. void apr_atomic_add(volatile apr_atomic_t *mem, apr_uint32_t val);
  112.  
  113. /**
  114.  * increment the atomic variable by 1
  115.  * @param mem pointer to the atomic value
  116.  */
  117. void apr_atomic_inc(volatile apr_atomic_t *mem);
  118.  
  119. /**
  120.  * decrement the atomic variable by 1
  121.  * @param mem pointer to the atomic value
  122.  * @returns zero if the value is zero, otherwise non-zero
  123.  */
  124. int apr_atomic_dec(volatile apr_atomic_t *mem);
  125.  
  126. /**
  127.  * compare the atomic's value with cmp.
  128.  * If they are the same swap the value with 'with'
  129.  * @param mem pointer to the atomic value
  130.  * @param with what to swap it with
  131.  * @param the value to compare it to
  132.  * @return the old value of the atomic
  133.  * @warning do not mix apr_atomic's with the CAS function.
  134.  * on some platforms they may be implemented by different mechanisms
  135.  */
  136. apr_uint32_t apr_atomic_cas(volatile apr_uint32_t *mem,long with,long cmp);
  137. #else /* !DOXYGEN */
  138.  
  139. #if APR_FORCE_ATOMIC_GENERIC 
  140. #if APR_HAS_THREADS
  141. #define APR_ATOMIC_NEED_DEFAULT 1
  142. #define APR_ATOMIC_NEED_CAS_DEFAULT 1
  143. #endif /* APR_HAS_THREADS */
  144.  
  145. #elif defined(WIN32)
  146.  
  147. #define apr_atomic_t LONG
  148.  
  149. #define apr_atomic_add(mem, val)     InterlockedExchangeAdd(mem,val)
  150. #define apr_atomic_dec(mem)          InterlockedDecrement(mem)
  151. #define apr_atomic_inc(mem)          InterlockedIncrement(mem)
  152. #define apr_atomic_set(mem, val)     InterlockedExchange(mem, val)
  153. #define apr_atomic_read(mem)         (*mem)
  154. #define apr_atomic_cas(mem,with,cmp) InterlockedCompareExchange(mem,with,cmp)
  155. #define apr_atomic_init(pool)        APR_SUCCESS
  156.  
  157. #elif defined(NETWARE)
  158.  
  159. #include <stdlib.h>
  160. #define apr_atomic_t unsigned long
  161.  
  162. #define apr_atomic_add(mem, val)     atomic_add(mem,val)
  163. APR_DECLARE(int) apr_atomic_dec(apr_atomic_t *mem);
  164. #define apr_atomic_inc(mem)          atomic_inc(mem)
  165. #define apr_atomic_set(mem, val)     (*mem = val)
  166. #define apr_atomic_read(mem)         (*mem)
  167. #define apr_atomic_init(pool)        APR_SUCCESS
  168. #define apr_atomic_cas(mem,with,cmp) atomic_cmpxchg(mem,with,cmp)
  169.  
  170. #elif defined(__FreeBSD__)
  171.  
  172. #include <machine/atomic.h>
  173.  
  174. #define apr_atomic_t apr_uint32_t
  175. #define apr_atomic_add(mem, val)     atomic_add_int(mem,val)
  176. #define apr_atomic_dec(mem)          atomic_subtract_int(mem,1)
  177. #define apr_atomic_inc(mem)          atomic_add_int(mem,1)
  178. #define apr_atomic_set(mem, val)     atomic_set_int(mem, val)
  179. #define apr_atomic_read(mem)         (*mem)
  180.  
  181. #define APR_ATOMIC_NEED_CAS_DEFAULT 1
  182.  
  183. #elif defined(__sparc__) || defined(sparc)
  184. #define apr_atomic_t apr_uint32_t
  185. #define apr_atomic_read(p)  *p
  186.  
  187. #define apr_atomic_add(mem, val)     apr_atomic_add_sparc(mem,val)
  188. #define apr_atomic_dec(mem)          apr_atomic_sub_sparc(mem,1)
  189. #define apr_atomic_inc(mem)          apr_atomic_add_sparc(mem,1)
  190. #define apr_atomic_cas(mem,val,cond) apr_atomic_cas_sparc(mem,val,cond)
  191. #define apr_atomic_set(mem, val)     (*mem = val)
  192. #define apr_atomic_init(pool)        APR_SUCCESS
  193.  
  194. apr_uint32_t apr_atomic_add_sparc(volatile apr_atomic_t *mem, apr_uint32_t add);
  195. apr_uint32_t apr_atomic_sub_sparc(volatile apr_atomic_t *mem, apr_uint32_t sub);
  196. apr_uint32_t apr_atomic_cas_sparc(volatile apr_uint32_t *mem, long with, long cmp);
  197.  
  198. #elif defined(__MVS__) /* OS/390 */
  199. #include <stdlib.h>
  200. #define apr_atomic_t cs_t
  201.  
  202. apr_int32_t apr_atomic_add(volatile apr_atomic_t *mem, apr_int32_t val);
  203. apr_uint32_t apr_atomic_cas(volatile apr_atomic_t *mem, apr_uint32_t swap, 
  204.                             apr_uint32_t cmp);
  205.  
  206. #define apr_atomic_inc(mem)          apr_atomic_add(mem, 1)
  207. #define apr_atomic_dec(mem)          apr_atomic_add(mem, -1)
  208. #define apr_atomic_init(pool)        APR_SUCCESS
  209.  
  210. /* warning: the following two operations, _read and _set, are atomic
  211.  * if the memory variables are aligned (the usual case).  
  212.  * 
  213.  * If you try really hard and manage to mis-align them, they are not 
  214.  * guaranteed to be atomic on S/390.  But then your program will blow up 
  215.  * with SIGBUS on a sparc, or with a S0C6 abend if you use the mis-aligned 
  216.  * variables with other apr_atomic_* operations on OS/390.
  217.  */
  218.  
  219. #define apr_atomic_read(p)           (*p)
  220. #define apr_atomic_set(mem, val)     (*mem = val)
  221.  
  222. #else
  223. #if APR_HAS_THREADS
  224. #define APR_ATOMIC_NEED_DEFAULT 1
  225. #define APR_ATOMIC_NEED_CAS_DEFAULT 1
  226. #endif /* APR_HAS_THREADS */
  227.  
  228. #endif /* !defined(WIN32) */
  229.  
  230. #if defined(APR_ATOMIC_NEED_DEFAULT)
  231. #define apr_atomic_t apr_uint32_t
  232. #define apr_atomic_read(p)  *p
  233. apr_status_t apr_atomic_init(apr_pool_t *p);
  234. void apr_atomic_set(volatile apr_atomic_t *mem, apr_uint32_t val);
  235. void apr_atomic_add(volatile apr_atomic_t *mem, apr_uint32_t val);
  236. void apr_atomic_inc(volatile apr_atomic_t *mem);
  237. int apr_atomic_dec(volatile apr_atomic_t *mem);
  238. #endif
  239.  
  240. #if defined(APR_ATOMIC_NEED_CAS_DEFAULT)
  241. apr_status_t apr_atomic_init(apr_pool_t *p);
  242. apr_uint32_t apr_atomic_cas(volatile apr_uint32_t *mem,long with,long cmp);
  243. #endif
  244.  
  245. #endif /* DOXYGEN */
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249.  
  250. #endif    /* !APR_ATOMIC_H */
  251.